package com.firword.common.domain.base;

import java.io.Serializable;
import java.time.Instant;

/**
 * @author: jingyajun
 * @date: 2020/9/23
 * @description: 实体类父类
 * @since:
 */
public class BaseDomain implements Comparable<BaseDomain>, Serializable {
    protected final static String SORT_ASC = "ASC";
    protected final static String SORT_DESC = "DESC";
    private String id;//主键
    private Boolean isActive;//是否有效
    private Integer order;//排序
    private String createBy;//创建人
    private String updateBy;//更新人
    private Instant created;//创建时间
    private Instant updated;//更新时间

    /**
     * 默认比较规则:
     * 1.创建日期倒序
     * 2.sort字段正序
     * 3.无论正序还是倒序,null最小
     *
     * @param o
     * @return this>param 1
     */
    @Override
    public int compareTo(BaseDomain o) {
        if (o == null) {
            return 1;
        }
        int compare = 0;
        compare = compareToCreated(o, BaseDomain.SORT_DESC);
        if (compare != 0) {
            return compare;
        }
        compare = compareToOrder(o, BaseDomain.SORT_ASC);
        return compare;
    }

    protected int compareToCreated(BaseDomain o, String sort) {
        Instant created1 = this.getCreated();
        Instant created2 = o.getCreated();
        if (created1 == null && created2 == null) {
            return 0;
        }
        if (created1 == null) {
            return -1;
        }
        if (created2 == null) {
            return 1;
        }
        int compare = created1.compareTo(created2);
        return BaseDomain.SORT_ASC.equalsIgnoreCase(sort) ? compare : compare * -1;
    }

    protected int compareToOrder(BaseDomain o, String sort) {
        Integer order1 = this.getOrder();
        Integer order2 = o.getOrder();
        if (order1 == null && order2 == null) {
            return 0;
        }
        if (order1 == null) {
            return -1;
        }
        if (order2 == null) {
            return 1;
        }
        int compare = order1.compareTo(order2);
        return BaseDomain.SORT_ASC.equalsIgnoreCase(sort) ? compare : compare * -1;
    }

    @Override
    public String toString() {
        return "BaseDoamin{" +
                "id='" + id + '\'' +
                ", isActive=" + isActive +
                ", order='" + order + '\'' +
                ", createBy='" + createBy + '\'' +
                ", updateBy='" + updateBy + '\'' +
                ", created=" + created +
                ", updated=" + updated +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Boolean getIsActive() {
        return isActive;
    }

    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public String getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }

    public Instant getCreated() {
        return created;
    }

    public void setCreated(Instant created) {
        this.created = created;
    }

    public Instant getUpdated() {
        return updated;
    }

    public void setUpdated(Instant updated) {
        this.updated = updated;
    }
}
